package edu.ilstu.mind.ise.ccp.logger;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import org.joda.time.LocalDateTime;

public abstract class AbstractLogger implements Logger {
	
	protected final String name;
	private final LoggerLevel loggerLevel;
	
	public AbstractLogger(String name, LoggerLevel level) {
		this.name = name;
		this.loggerLevel = level;
	}

	protected String getThrowableAsString(Throwable t) {
		final Writer result = new StringWriter();
	    final PrintWriter printWriter = new PrintWriter(result);
	    t.printStackTrace(printWriter);
	    return result.toString();
	}

	protected String getTimestamp() {
		return new LocalDateTime().toString();
	}

	protected boolean isEnabled(LoggerLevel level) {
		return level.isLoggable(loggerLevel);
	}

	@Override
	public boolean isTraceEnabled() {
		return isEnabled(LoggerLevel.TRACE);
	}

	@Override
	public boolean isDebugEnabled() {
		return isEnabled(LoggerLevel.DEBUG);
	}
	
	@Override
	public boolean isInfoEnabled() {
		return isEnabled(LoggerLevel.INFO);
	}

	@Override
	public boolean isWarnEnabled() {
		return isEnabled(LoggerLevel.WARN);
	}

	@Override
	public boolean isErrorEnabled() {
		return isEnabled(LoggerLevel.ERROR);
	}

	@Override
	public boolean isFatalEnabled() {
		return isEnabled(LoggerLevel.FATAL);
	}
}
